package com.smart_campus.teg.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.smart_campus.sys.common.DataGridView;
import com.smart_campus.sys.domain.Role;
import com.smart_campus.teg.domain.Course;
import com.smart_campus.teg.domain.Major;
import com.smart_campus.teg.domain.TegClass;
import com.smart_campus.teg.mapper.CourseMapper;
import com.smart_campus.teg.mapper.PersonnerMapper;
import com.smart_campus.teg.service.PersonnelService;
import com.smart_campus.teg.vo.ClassVo;
import com.smart_campus.teg.vo.CourseVo;
import com.smart_campus.teg.vo.StudentVo;
import com.smart_campus.teg.vo.TeacherVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @aytgor 陈鹏
 * @date 2020/6/20 9:07
 */
@Service
public class PersonnelServiceImpl implements PersonnelService {
    @Resource PersonnerMapper personnerMapper;
    @Resource CourseMapper courseMapper;

    @Override
    public DataGridView TeacherList(TeacherVo teacherVo) {
        Page<Object> page = PageHelper.startPage(teacherVo.getPage(), teacherVo.getLimit());//分页数据格式
        List<TeacherVo> noticeVos = personnerMapper.TeacherList(teacherVo);//返回的全部数据
        return new DataGridView(page.getTotal(), noticeVos);
    }

    @Override
    public DataGridView StudentList(StudentVo studentVo) {
        Page<Object> page = PageHelper.startPage(studentVo.getPage(), studentVo.getLimit());//分页数据格式
        List<StudentVo> noticeVos = personnerMapper.StudentList(studentVo);//返回的全部数据
        return new DataGridView(page.getTotal(), noticeVos);
    }

    @Override
    public DataGridView CourseTeacher(Integer id) {
        //查询全部的课程
        List<Course> courses = this.courseMapper.CourseList(new CourseVo());
        //根据老师ID查询要教授的课程ID
        List<Integer> coursesIds=this.personnerMapper.getCid(id);
        //创建返回list
        List<Map<String,Object>> lists=new ArrayList<>();
        //循环判断老师课程有那些并赋值为true
        for (Course course : courses) {
            Boolean LAY_CHECKED=false;
            for (Integer roleId : coursesIds) {
                if(course.getCourseId().equals(roleId)){
                    LAY_CHECKED=true;
                    break;
                }
            }
            Map<String,Object> map=new HashMap<>();
            map.put("id",course.getCourseId());
            map.put("name",course.getCourseName());
            map.put("remark",course.getCourseRemark());
            map.put("LAY_CHECKED",LAY_CHECKED);
            lists.add(map);
        }
        return new DataGridView(Long.valueOf(lists.size()),lists);
    }

    @Override
    public void setCourseTeacher(Integer uid, Integer[] Cids) {
        this.personnerMapper.DeleteCourseTeacher(uid);
        if (Cids != null && Cids.length > 0) {
            for (Integer cid : Cids) {
                this.personnerMapper.InsertCourseTeacher(uid,cid);
            }
        }
    }

    @Override
    public DataGridView getStudentClass(Integer id) {
        List<ClassVo> tegClasses = this.personnerMapper.getStudentClass();
        Integer classid=this.personnerMapper.getStudentClass1(id);
        List<Map<String,Object>> lists=new ArrayList<>();
        for (ClassVo classVo : tegClasses) {
            Boolean LAY_CHECKED=false;
                if(classVo.getClassId().equals(classid)){
                    LAY_CHECKED=true;
                }
            Map<String,Object> map=new HashMap<>();
            map.put("id",classVo.getClassId());
            map.put("name",classVo.getClassRemark());
            map.put("remark",classVo.getMajorname());
            map.put("LAY_CHECKED",LAY_CHECKED);
            lists.add(map);
        }
        return new DataGridView(Long.valueOf(lists.size()),lists);
    }

    @Override
    public void setStudentClass(Integer uid, Integer cid) {
        personnerMapper.deleteStudentClass(uid);
        personnerMapper.insertStudentClass(uid,cid);
    }

}
